<?php


namespace App\Services;

use App\Models\Product;
use \Image;
class ImageService
{

    public function resize($path,$size){
        if(is_file($path)){
            try {
                $img = Image::make($path)->resize($size, null, function ($constraint) {
                    $constraint->aspectRatio();
                });
                $saveName = $img->filename.'-'.$size.'.'.$img->extension;
                $img->save($img->dirname.'/'.$saveName);
            }catch (\Exception $exception){

            }

        }
    }

    public function resize_product(Product $product){

        $path = public_path('uploads/'.ltrim($product->img,'/'));
        if(file_exists($path)){
            $this->resize($path,50);
        }

    }

}
